import * as path from 'path';
import { Global, Module } from '@nestjs/common';
import { LoggerModule as PinoLoggerModule } from 'nestjs-pino';
import * as dayjs from 'dayjs';
import { LoggerController } from './logger.controller';
import { LoggerService } from './logger.service';


@Global()
@Module({
  imports: [
    PinoLoggerModule.forRoot({
      pinoHttp: {
        autoLogging: true,
        transport: {
          targets: [
            // 1. 普通日志传输器 (info, debug, warn)
            {
              target: 'pino-roll',
              level: 'info', // 捕获 info 及以上级别
              options: {
                file: path.join('logs/info', `info-${dayjs().format('YYYY-MM-DD_HH-mm-ss')}.log`),
                frequency: 'daily', // 每天轮换
                mkdir: true, // 自动创建目录
                maxSize: '10m', // 每个文件最大10MB
                maxFiles: '7d', // 保留7天
                extension: '.log',
                encoding: 'utf-8',
              },
            },
            // 2. 错误日志传输器 (error, fatal)
            {
              target: 'pino-roll',
              level: 'error', // 只捕获 error 和 fatal 级别
              options: {
                // 使用dayjs 格式化日期
                file: path.join('logs/error', `error-${dayjs().format('YYYY-MM-DD_HH-mm-ss')}.log`),
                frequency: 'daily',
                mkdir: true,
                maxSize: '10m',
                maxFiles: '30d', // 错误日志保留30天
                extension: '.log',
                encoding: 'utf-8',
              },
            },
          ],
        },
        level: 'debug', // 设置日志级别
      },
    }),
  ],
  controllers: [LoggerController],
  providers: [LoggerService],
  exports: [LoggerService],
})
export class LoggerModule {}
